Hop til hovedindhold
Document status35 - Reviewed

Server Validation Script for PSQL VM

This document describes the PostgreSQL health validation script used to verify that the database server is functioning correctly after deployments, maintenance operations, or as part of routine health checks.

Overview

The validation script connects to the PostgreSQL VM via SSH and performs a comprehensive series of checks to ensure the database is operational, accessible, and performing within expected parameters. The script runs automatically as part of CI/CD pipelines on a self-hosted agent with direct network access to the database VM.

How to Run

To execute the server validation:

  1. Open the pipeline - Navigate to Server Validation - PSQL in Azure DevOps
  2. Start the pipeline - Click Run Pipeline in the top right corner
  3. Select environment - Choose which environment to validate from the dropdown
  4. Monitor the results - Review the pipeline output to see each validation check and its pass/fail status

Validation Checks

The script performs the following health checks in sequence:

Check 1: PostgreSQL Service Status

Verifies that the PostgreSQL service is running on the VM.

AspectDetails
Commandsystemctl is-active postgresql
Pass ConditionService is active and running
Failure ImpactCritical - indicates the database server is down

Check 2: Database Exists

Confirms that the application database exists in PostgreSQL.

AspectDetails
CommandQuery pg_database system catalog
Pass ConditionDatabase is found in the catalog
Failure ImpactCritical - application cannot function without the database

Check 3: Accepting Connections

Verifies that PostgreSQL is ready to accept incoming connections.

AspectDetails
Commandpg_isready utility
Pass ConditionPostgreSQL reports ready status
Failure ImpactCritical - applications cannot connect to the database

Check 4: Active Connections

Counts the number of active database connections and total connections.

AspectDetails
Querypg_stat_activity system view
Metrics ReportedActive queries, total connections
PurposeInformational - helps identify connection pool issues

Check 5: Application Connections

Verifies that application services have established connections to the database.

AspectDetails
QueryCount connections from application database users
Pass ConditionConnections meet minimum expected threshold
Failure ImpactFail - applications are not connected to the database

This check examines connections from application-specific database users (such as writer and reader accounts) to ensure that the application layer is properly connected to the database.

Check 6: Query Response Test

Executes a simple query to verify the database responds to commands.

AspectDetails
QuerySELECT 1
Metrics ReportedQuery execution time in milliseconds
Pass ConditionQuery returns expected result
Failure ImpactCritical - indicates database is unresponsive

Check 7: Connection Details

Provides detailed breakdown of database connections for diagnostic purposes.

InformationDescription
Connections by UserShows connection count per database user with active/idle breakdown
Connections by Client AddressShows which IP addresses have connections to the database

This information is useful for identifying:

  • Which services are connected
  • Connection pool behavior
  • Potential connection leaks

Check 8: Disk Space

Monitors disk usage on the PostgreSQL VM to prevent space-related issues.

AspectDetails
Commanddf -h on data volumes
Threshold85% usage
Failure ImpactFail - database may become read-only or crash if disk fills up

The check examines all mounted data volumes and reports:

  • Filesystem name
  • Total size
  • Used space
  • Available space
  • Usage percentage
  • Mount point

Check 9: WAL-G Backup Status (Optional)

When enabled, validates that the backup system is properly configured and functioning.

AspectDetails
Backup ToolWAL-G
Checks PerformedExecutable exists, configuration present, cron job scheduled, recent backups exist
Pass ConditionLatest backup within 48 hours

This check verifies:

  • WAL-G executable is installed at expected location
  • Configuration file exists
  • Backup cron job is scheduled for the postgres user
  • Recent backups can be listed
  • Latest backup is within acceptable age threshold

Note: Backup checks are disabled by default and must be explicitly enabled for environments where backups are configured.

Check 10: Passwordless Sudo

Verifies that the VM admin user can run sudo su without being prompted for a password.

AspectDetails
Commandsudo -n true
Pass Conditionsudo executes without password prompt
Failure ImpactFail - password rotation and other automated operations will break

The VMAccessForLinux extension can create /etc/sudoers.d/waagent during password rotation, which overrides the NOPASSWD rule from cloud-init. The password rotation script automatically removes this file after rotating passwords to prevent this issue.

Validation Summary

After all checks complete, the script outputs a summary with the status of each validation:

Result VariableDescription
SERVICE_RUNNINGPostgreSQL service is active
DATABASE_EXISTSApplication database exists
ACCEPTING_CONNECTIONSDatabase is accepting connections
TOTAL_CONNECTIONSTotal number of database connections
APP_CONNECTIONSNumber of application service connections
QUERY_RESPONSEDatabase responds to queries
DISK_OKAll disks below 85% threshold
SUDO_NOPASSWDPasswordless sudo is working
BACKUP_CONFIGUREDBackup system is operational (when enabled)
VALIDATION_PASSEDOverall validation result

Exit Codes

CodeMeaning
0All validations passed - database is healthy
1One or more validations failed - review output for details

Troubleshooting

Common Issues

SymptomPossible CauseResolution
SSH connection failsIncorrect credentials, network issueCheck Key Vault for correct password, verify agent network access
Service not runningVM was restarted, service crashedCheck VM status, review PostgreSQL logs
No application connectionsContainer Apps not running, connection string issuesVerify Container Apps are healthy, check connection string secrets
Disk space criticalLog files, WAL accumulation, data growthReview and clean logs, verify backup archiving
Query timeoutDatabase overloaded, long-running queriesCheck pg_stat_activity for blocking queries